<?php

final class AccountModel extends AbstractModel {

    protected $validation = array(
        'name' => array(
        ),
        'password' => array(
            'type' => 'password'
        )
    );

    public function makeLogged() {
        $_SESSION['account'] = array(
            'logged' => true,
            'id' => $this->getId(),
            'name' => $this->getName()
        );
    }

    public static function isLogged() {
        if (isset($_SESSION['account'])) {
            if (isset($_SESSION['account']['logged'])) {
                return (bool) $_SESSION['account']['logged'];
            }
        }
        return false;
    }

    public static function logout() {
        $_SESSION['account'] = array();
    }

    public function login() {
        $criteria = new SQLCriteria();
        $criteria->add(new SQLFilter('name', '=', $this->getName()));
        $criteria->add(new SQLFilter('password', '=', $this->getPassword()));
        $criteria->setProperty('limit', 1);

        $sql = new SQLSelect();
        $sql->setEntity($this->getEntity());
        $sql->addColumn('id');
        $sql->addColumn('name');
        $sql->setCriteria($criteria);

        $result = $sql->execute();
        if ($result->rowCount() > 0) {
            $row = $result->fetch(PDO::FETCH_ASSOC);
            $this->fromArray($row);
            return true;
        } else {
            return false;
        }
    }

}

?>
